<?php
namespace Home\Controller;

use Think\Controller;

class NoticeController extends Controller
{
    public function index()
    {
        if(!D('User')->checklogin()) return $this->error('请先登录',U('User/login'),1);
        if(!U('Public/'))
            $this->error('您尚未登录',U('User/login'));
        $user = D('user')->getUserByUsername($_SESSION['uid']);
//        dump($user);
        //第一种写法  比较低效
        $messages = [];
        $message_notices = D('notice')->getMessage_NoticeByToId($user[0]['id']);
//        dump($message_notices);
        foreach ($message_notices as $key => $notice) {
            D('message')->where('id ');
            $from_id = $notice['from_id'];
            if ($messages[$from_id]['to_id'] == $user[0]['id']) {//判断是否已在通知队列中 同一个对象发过来的
            } else {
                $messages[$from_id]['to_id'] = $user[0]['id'];
            }
//            if ($notice['view_status'] == 'false') {//判断是否为未提醒数据
                $messages[$from_id]['notice_count']++;
                $message_content = D('message')->getMessageById($notice['notice_id']);
                $messages[$from_id]['last_message'] = $message_content[0]['content'];
                $messages[$from_id]['create_time'] = $message_content[0]['create_time'];
//            }
        }
        foreach ($messages as $key => $message) {
            $from_user = D('User')->getUserById($key);
            $messages[$key]['from_name'] = $from_user[0]['username'];
            $avatar_data = D('Avatar')->getAvatarById($key);
            $messages[$key]['avatar_name'] = $avatar_data[0]['name'];
        }
        $unviewed_comments_notices=D('Notice')->getUnViewedComment_NoticeByToId($user[0]['id']);
//        dump($unviewed_comments_notices);
        foreach($unviewed_comments_notices as $key=>$unviewed_comments_notice){
            $comment=D('Comment')->getCommentById($unviewed_comments_notice['notice_id']);
//            dump($comment);
            $unviewed_comments_notices[$key]['comment_id']=$comment[0]['id'];
            $unviewed_comments_notices[$key]['article_id']=$comment[0]['article_id'];
            $unviewed_comments_notices[$key]['content']=$comment[0]['content'];
            $unviewed_comments_notices[$key]['create_time']=$comment[0]['create_time'];
            $avatar_data=D('Avatar')->getAvatarById($comment[0]['from_id']);
            $unviewed_comments_notices[$key]['avatar_name']=$avatar_data[0]['name'];
//            dump($unviewed_comments_notices[0]['article_id']);die();
            $article=D('Article')->getArticleById($unviewed_comments_notices[$key]['article_id']);
            $unviewed_comments_notices[$key]['title']=$article[0]['title'];
        }

        $viewed_comments_notices=D('Notice')->getViewedComment_NoticeByToId($user[0]['id']);
        foreach($viewed_comments_notices as $key=>$viewed_comments_notice){
            $comment=D('Comment')->getCommentById($viewed_comments_notice['notice_id']);
            $viewed_comments_notices[$key]['comment_id']=$comment[0]['id'];
            $viewed_comments_notices[$key]['article_id']=$comment[0]['article_id'];
            $viewed_comments_notices[$key]['content']=$comment[0]['content'];
            $viewed_comments_notices[$key]['create_time']=$comment[0]['create_time'];
            $avatar_data=D('Avatar')->getAvatarById($comment[0]['from_id']);
            $viewed_comments_notices[$key]['avatar_name']=$avatar_data[0]['name'];
            $article=D('Article')->getArticleById($viewed_comments_notices[$key]['article_id']);
            $viewed_comments_notices[$key]['title']=$article[0]['title'];
        }
//        dump($messages);
//        dump($unviewed_comments_notices);
//        dump($viewed_comments_notices);
        $this->assign('messages', $messages);
        $this->assign('unviewed_comments_notices', $unviewed_comments_notices);
        $this->assign('viewed_comments_notices', $viewed_comments_notices);
        $this->display(T('Home@Notice/index'));
    }


}